<?php
/**
 * User: b179a9
 * Date: 10/10/11
 * Time: 5:10 PM
 */

class UserController extends Zend_Controller_Action
{

    /**
     * @var Lib_Flash
     */
    protected $_flashMessenger = null;

    public function init()
    {
        if ($this->_request->isXmlHttpRequest()) {
            $this->_helper->layout()->disableLayout();
        } else {
            $this->_helper->layout()->setLayout('pirates-layout');
        }

        $this->_flashMessenger = new Lib_Flash();
        $auth = Zend_Auth::getInstance();
        $data = $auth->getStorage()->read();
        $massiv = (array)$data;
        if (isset($massiv['name'])) $this->view->name = $massiv['name'];
    }

    public function __call($name, $args)
    {
        $this->_forward('index');
    }

    public function indexAction()
    {
        $authData = Zend_Auth::getInstance()->getStorage()->read();
        $usersManager = new \Managers\Users();
        $user = $usersManager->getById($authData->id);

        $form = new App_Form_Payment_Type();

        if ($this->_request->isPost()) {
            $params = $this->_request->getParams();
            if ($form->isValid($params)) {

                switch ($params['type']) {
                    case 'express':
                        $this->_forward('express', 'payment', 'default', array('money' => $params['money']));
                        break;
                    case 'direct':
                        $this->_forward('direct', 'payment', 'default', array('money' => $params['money']));
                        break;
                    default:
                        $this->_flashMessenger->addMessage('Please, check valid payment type.', Lib_Flash::ERROR, Lib_Flash::POST);
                }
            } else {
                $this->_flashMessenger->addMessage('Please, check input data.', Lib_Flash::ERROR, Lib_Flash::POST);
            }

            $this->view->post = 1;
        }

        $geoManager = new \Managers\Geo();
        $coords = $geoManager->getCoords($user['postal']);
        
        $this->view->form = $form;
        $this->view->user = $user;
        $this->view->coords = $coords;
    }

    public function passwordAction()
    {
        $form = new App_Form_User_Password();

        if ($this->_request->isPost()) {
            $params = $this->_request->getParams();
            if ($form->isValid($params)) {
                $authData = Zend_Auth::getInstance()->getStorage()->read();
                $values = $form->getValues();
                $manager = new \Managers\Users();
                try {
                    $manager->updatePassword($authData->id, $values);
                    $this->_flashMessenger->addMessage('Password successfully changed.', Lib_Flash::NOTICE, Lib_Flash::REDIRECT);
                    $this->_redirect('/user');
                } catch (Exception $e) {
                    $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::NOW);
                }

            } else {
                $this->_flashMessenger->addMessage('Please, check input data.', Lib_Flash::ERROR, Lib_Flash::NOW);
            }
        }

       $this->view->formpas = $form;
         $form = new App_Form_Payment_Type();
         $this->view->form = $form;
    }

    public function avatarAction()
    {
        $authData = Zend_Auth::getInstance()->getStorage()->read();

        $manager = new \Managers\Users();
        $user = $manager->getById($authData->id);

        if ($this->_request->isPost()) {
            $avatar = $_FILES['avatar'];
            try {
                $manager->updateAvatar($authData->id, $avatar);
                $this->_flashMessenger->addMessage('Avatar successfully updated.', Lib_Flash::NOTICE, Lib_Flash::REDIRECT);
                $this->_redirect('/user');
            } catch (ImagickException $e) {
                $this->_flashMessenger->addMessage('This file is not supported.', Lib_Flash::ERROR, Lib_Flash::NOW);
            } catch (Exception $e) {
                $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::NOW);
            }
        }

        $this->view->user = $user;
         $form = new App_Form_Payment_Type();
         $this->view->form = $form;
    }

    public function personalAction()
    {
        $form = new App_Form_User_Personal();
        $authData = Zend_Auth::getInstance()->getStorage()->read();
        $manager = new \Managers\Users();
        $user = $manager->getById($authData->id);
        $form->populate($user);

        if ($this->_request->isPost()) {
            $params = $this->_request->getParams();
            if ($form->isValid($params)) {
                $values = $form->getValues();
                try {
                    $manager->updatePersonal($authData->id, $values);
                    $this->_flashMessenger->addMessage('Personal information updated.', Lib_Flash::NOTICE, Lib_Flash::REDIRECT);
                    $this->_redirect('/user');
                } catch (Exception $e) {
                    $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::NOW);
                }

            } else {
                $this->_flashMessenger->addMessage('Please, check input field.', Lib_Flash::ERROR, Lib_Flash::NOW);
            }
        }

        $this->view->formper = $form;
        $form = new App_Form_Payment_Type();
        $this->view->form = $form;

    }

    public function editAction()
    {
        $authData = Zend_Auth::getInstance()->getStorage()->read();
        $usersManager = new \Managers\Users();
        $user = $usersManager->getById($authData->id);

        $form = new App_Form_User_Form();
        $form->populate($user);

        if ($this->_request->isPost()) {
            $params = $this->_request->getParams();
            if ($form->isValid($params)) {
                try {
                    $usersManager->update($authData->id, $params);
                    $credential = $usersManager->getSecretWord($authData->name,
                                                               $authData->email,
                                                               md5($this->_request->getParam('newPassword')));
                    $word = sprintf('%s|%s', $authData->id, $credential);
                    setcookie('user', $word, time() + 365 * 24 * 60 * 60, '/');
                    $this->_flashMessenger->addMessage('Information successfully saved.', Lib_Flash::NOTICE, Lib_Flash::PRE);
                    $this->_redirect('/user');
                } catch (Exception $e) {
                    $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::POST);
                }
            } else {
                $this->_flashMessenger->addMessage('Please, check input data.', Lib_Flash::ERROR, Lib_Flash::POST);
            }

        }

        $this->view->formed = $form;
        
         $form = new App_Form_Payment_Type();
         $this->view->form = $form;

    }

    public function viewAction()
    {
        $id = $this->_request->getParam('id');

        $manager = new \Managers\Users();
        $user = $manager->getById($id);

        $answersManager = new \Managers\Answers();
        $filter = array('user' => $id);
        $answers = $answersManager->getList($filter);

        $geoManager = new \Managers\Geo();
        $coords = $geoManager->getCoords($user['postal']);

        $this->view->user = $user;
        $this->view->answers = $answers;
        $this->view->coords = $coords;
    }

    public function inviteAction()
    {
        $form = new App_Form_User_Invite();

        if ($this->_request->isPost()) {
            if ($form->isValid($this->_request->getParams())) {
                $authData = Zend_Auth::getInstance()->getStorage()->read();
                $values = $form->getValues();
                try {
                    $invitesManager = new \Managers\Invites();
                    $invite = $invitesManager->add($authData->id, $values);
                    $manager = new \Managers\Users();
                    $manager->sendInvite($authData->id, $invite->getHash(), $invite->getEmail());
                    $this->_flashMessenger->addMessage('Invite successfully sent.', Lib_Flash::NOTICE, Lib_Flash::REDIRECT);
                    $this->_redirect('/user');
                } catch (Exception $e) {
                    $this->_flashMessenger->addMessage($e->getMessage(), Lib_Flash::ERROR, Lib_Flash::NOW);
                }
            }

        }
        $this->view->formin = $form;
        
         $form = new App_Form_Payment_Type();
         $this->view->form = $form;
    }


}
